package handler

import (
	"api/pkg"
	__ "api/proto"
	"api/request"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
)

func Login(c *gin.Context) {
	var req request.Login
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	sendsms, err := c1.Login(c, &__.LoginRequest{
		Mobile:  req.Mobile,
		Sendsms: req.Sendsms,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "登录失败",
			"data": err.Error(),
		})
		return
	}
	token, err := pkg.NewJWT("2211a").CreateToken(pkg.CustomClaims{ID: uint(sendsms.Id)})
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"msg":  "登录成功",
		"data": token,
	})
}

func Sendsms(c *gin.Context) {
	var req request.Sendsms
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	sendsms, err := c1.Sendsms(c, &__.SendsmsRequest{
		Mobile: req.Mobile,
		Score:  req.Score,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "发送短信失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"msg":  "发送短信成功",
		"data": sendsms,
	})
}

func List(c *gin.Context) {
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	sendsms, err := c1.ListWork(c, &__.ListWorkRequest{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "展示失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": sendsms,
	})
}

func AddWork(c *gin.Context) {
	var req request.AddWork
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	sendsms, err := c1.AddWork(c, &__.AddWorkRequest{
		Title:          req.Title,
		Desc:           req.Desc,
		MusicId:        int64(req.Musicid),
		WorkType:       req.Worktype,
		CheckStatus:    req.Checkstatus,
		IpAddress:      req.Ipaddress,
		WorkPermission: req.Workpermission,
		LikeCount:      int64(req.Likecount),
		CommentCount:   int64(req.Collectcount),
		ShareCount:     int64(req.Sharecount),
		CollectCount:   int64(req.Collectcount),
		BrowseCount:    int64(req.Browsecount),
		TopicId:        int64(req.Topicid),
		UserId:         int64(req.Musicid),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "发布作品失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"msg":  "发布作品成功 待审核",
		"data": sendsms,
	})
}

func Comment(c *gin.Context) {
	var req request.Comment
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	sendsms, err := c1.Comment(c, &__.CommentRequest{
		WorkId:  int64(req.Workid),
		Content: req.Content,
		Tag:     int64(req.Tag),
		Pid:     int64(req.Pid),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "评论失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"msg":  "点赞成功",
		"data": sendsms,
	})
}
